package com.pet.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

/**
 * 序列生成方法.
 * @author King
 * @since 2011.07.02
 */
public final class CreateSequencesUtil {
    
    /**
     * 构造函数
     */
    private CreateSequencesUtil() {
    }
    
    private static long sSequenceNo = 1;
    
    public static final long MAX_SIZE = 10000;

    public static final Object LOCK = new Object();
    
    /**
     * 创建序列.
     * @param key 自定义序列KEY
     * @param session session
     * @return 序列
     */
    public static String createID(String key) {
    	DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); 
        final String tranDate = df.format(GregorianCalendar.getInstance().getTime());
        int current = 10;
        synchronized (LOCK) {
            if (sSequenceNo > MAX_SIZE) {
                sSequenceNo = 10;
            }
            current += sSequenceNo++;
        }
        final StringBuffer sb = new StringBuffer(key);
        sb.append("0000");
        sb.append(tranDate).append(current);
        
        return sb.toString();
    }
    
    /** 
    * @Description: 主函数
    * @author king
    * @since Oct 5, 2012 11:01:56 AM 
    * 
    * @param args 参数
    */
    public static void main(String[] args) {
        System.out.println(createID("UB"));
    }
}
